package org.fund.base_service.service.impl;

import org.fund.base_service.domain.OptionalFund;
import org.fund.base_service.domain.vo.OptionalFundVO;
import org.fund.base_service.mapper.OptionalFundMapper;
import org.fund.base_service.service.OptionalFundService;
import org.fund.common.model.FundEstimate;
import org.fund.common.model.FundValue;
import org.fund.common.util.DateUtils;
import org.fund.common.util.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 自选基金(OptionalFund)表服务实现类
 *
 * @author makejava
 * @since 2022-03-13 01:28:44
 */
@Service
public class OptionalFundServiceImpl implements OptionalFundService {
    @Autowired
    private OptionalFundMapper optionalFundMapper;
    @Autowired
    private FundService fundService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OptionalFundVO queryById(Integer id) {
        OptionalFund fund = this.optionalFundMapper.queryById(id);
        if (!Objects.isNull(fund)){
            return   getOptionalFundVO(fund);
        }
        return null;

    }

    /**
     *
     * @return 查询结果
     */
    @Override
    public List<OptionalFundVO> queryAll() {
        List<OptionalFund> options = optionalFundMapper.queryAll(SecurityUtils.getUserId());
        if (CollectionUtils.isEmpty(options)){
            return  Collections.emptyList();
        }
        List<OptionalFundVO> vos = new ArrayList<>(options.size());
        for (OptionalFund option : options) {
            OptionalFundVO vo = getOptionalFundVO(option);
            vos.add(vo);
        }
        return vos;
    }

    private OptionalFundVO getOptionalFundVO(OptionalFund option) {
        FundEstimate jzEstimate = fundService.getJZEstimate(option.getFundCode());
        OptionalFundVO vo = new OptionalFundVO();
        BeanUtils.copyProperties(option, vo);
        vo.setFundEstimate(jzEstimate);
        return vo;
    }

    /**
     * 新增数据
     *
     * @param optionalFund 实例对象
     * @return 实例对象
     */
    @Override
    public OptionalFund insert(OptionalFund optionalFund) {
        optionalFund.setUId(SecurityUtils.getUserId());
        optionalFundMapper.insert(optionalFund);
        return optionalFund;
    }

    /**
     * 修改数据
     *
     * @param optionalFund 实例对象
     * @return 实例对象
     */
    @Override
    public OptionalFund update(OptionalFund optionalFund) {
        this.optionalFundMapper.update(optionalFund);
        return this.queryById(optionalFund.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.optionalFundMapper.deleteById(id) > 0;
    }

    @Override
    public List<FundValue> getHistoryJZ(String fundCode, int pageSize, String endDate) {
        String startDate = null;
        try {
            startDate = DateUtils.getDay(endDate, pageSize);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return fundService.lsjz(fundCode, pageSize, startDate,endDate);
    }
}
